package com.sxt.sys.listener;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sxt.sys.domain.User;
import com.sxt.sys.service.UserService;
import com.sxt.sys.utils.SessionUtils;

//当当前任务启动时,任务监听触发指定当前办理人的上级领导
//使用任务监听来指定部门经理,以及总领领导的任务办领人
public class DivisionTaskListenerImpl implements TaskListener {

	@Override
	public void notify(DelegateTask delegate) {
		// TODO Auto-generated method stub
		//delegate.setAssignee(arg0);
		//得到当前用户信息
		User currentUser= SessionUtils.getCurrentUser();
		//取出领导ID
		Integer mgr=currentUser.getMgr();
		//取出IOC容器
		HttpServletRequest request=SessionUtils.getCurrentServletRequest();
		ApplicationContext applicationContext=WebApplicationContextUtils
				.getWebApplicationContext(request.getServletContext());
		//从IOC容器里面取出UserService
		UserService userService=applicationContext.getBean(UserService.class);
		//查询当前用户的领导信息
	    User leadUser=userService.queryUserById(mgr);
	    //设置办理人
	    delegate.setAssignee(leadUser.getName());
	}

}
